package com.palfund.glide.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;

import java.lang.ref.WeakReference;

/**
 * Created by clvc on 2017/8/27.
 * 真正的速度是看不见的 !
 * Today is today , we will go !
 */

public class RoundImageView extends AppCompatImageView {

    // 数据定义
    private Paint mPaint;
    private Xfermode mXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
    private Bitmap mMaskBitmap;
    private float mRoundBorderRadius;// 圆角大小
    private int mType;// 类型：圆形、圆角或椭圆

    private WeakReference<Bitmap> mBufferBitmap;// 使用缓存技术，避免每次都执行onDraw
    /**
     * 圆
     */
    public static final int TYPE_CIRCLE = 1;
    /**
     * 四角圆，矩形
     */
    public static final int TYPE_ROUND = 2;
    /**
     * 椭圆形
     */
    public static final int TYPE_OVAL = 3;
    private static final int DEFAULT_ROUND_BORDER_RADIUS = 10;// 默认圆角大小

    // 构造方法
    public RoundImageView(Context context) {
        this(context, null);
    }

    public RoundImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);// 设置消除锯齿
        mType = TYPE_CIRCLE;
        mRoundBorderRadius = DEFAULT_ROUND_BORDER_RADIUS;
    }

    /**
     * 测量view的大小
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // 如果类型是圆形，则强制设置view的宽高一致，取宽高的较小值
        if (mType == TYPE_CIRCLE) {
            int width = Math.min(getMeasuredWidth(), getMeasuredHeight());
            setMeasuredDimension(width, width);
        }
    }

    /**
     * 绘制view的内容
     */
    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        // 从缓存中取出bitmap
        Bitmap bmp = (mBufferBitmap == null ? null : mBufferBitmap.get());
        if (bmp == null || bmp.isRecycled()) {
            // 如果没有缓存存在的情况
            // 获取drawable
            Drawable drawable = getDrawable();
            if (null != drawable) {
                // 获取drawable的宽高
                int dwidth = drawable.getIntrinsicWidth();
                int dheight = drawable.getIntrinsicHeight();
                bmp = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
                float scale = 1.0f;
                // 创建画布
                Canvas drawCanvas = new Canvas(bmp);
                // 按照bitmap的宽高，以及view的宽高，计算缩放比例；因为设置的src宽高
                // 比例可能和imageview的宽高比例不同，这里我们不希望图片失真；

                if (mType == TYPE_CIRCLE) {// 如果是圆形
                    scale = getWidth() * 1.0F / Math.min(dwidth, dheight);
                } else if (mType == TYPE_ROUND || mType == TYPE_OVAL) {// 如果是圆角矩形或椭圆
                    // 如果图片的宽或者高与view的宽高不匹配，计算出需要缩放的比例；
                    // 缩放后的图片的宽高，一定要大于我们view的宽高；所以我们这里取大值；
                    scale = Math.max(getWidth() * 1.0f / dwidth, getHeight() * 1.0f / dheight);
                }
                // 根据缩放比例，设置bounds，即相当于做缩放图片
                drawable.setBounds(0, 0, (int) (scale * dwidth), (int) (scale * dheight));
                drawable.draw(drawCanvas);
                // 获取bitmap，即圆形、圆角或椭圆的bitmap
                if (mMaskBitmap == null || mMaskBitmap.isRecycled()) {
                    mMaskBitmap = getDrawBitmap();
                }
                // 为paint设置Xfermode 渲染模式
                mPaint.reset();
                mPaint.setFilterBitmap(false);
                mPaint.setXfermode(mXfermode);
                // 绘制不同形状
                if (mMaskBitmap != null) {
                    drawCanvas.drawBitmap(mMaskBitmap, 0, 0, mPaint);
                }
                mPaint.setXfermode(null);

                // 将准备好的bitmap绘制出来
                canvas.drawBitmap(bmp, 0, 0, null);
                // bitmap缓存起来，避免每次调用onDraw，分配内存
                mBufferBitmap = new WeakReference<Bitmap>(bmp);
            }

        } else {
            // 如果缓存还存在的情况
            mPaint.setXfermode(null);
            canvas.drawBitmap(bmp, 0.0f, 0.0f, mPaint);
            return;
        }
    }

    /**
     * 绘制不同的图形Bitmap
     */
    private Bitmap getDrawBitmap() {
        Bitmap bitmap = null;
        try {
            bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(Color.BLACK);

            if (mType == TYPE_CIRCLE) {// 绘制圆形
                canvas.drawCircle(getWidth() / 2, getWidth() / 2, getWidth() / 2, paint);
            } else if (mType == TYPE_ROUND) {// 绘制圆角矩形
                canvas.drawRoundRect(new RectF(0, 0, getWidth(), getHeight()),
                        mRoundBorderRadius, mRoundBorderRadius, paint);
            } else if (mType == TYPE_OVAL) {
                // 绘制椭圆
                canvas.drawOval(new RectF(0, 0, getWidth(), getHeight()), mPaint);
            }
        } catch (Throwable e) {
        }

        return bitmap;
    }

    /**
     * 因为使用了缓存技术，所以需要在invalidate中做些回收释放资源的处理
     */
    @Override
    public void invalidate() {
        mBufferBitmap = null;
        if (mMaskBitmap != null) {
            mMaskBitmap.recycle();
            mMaskBitmap = null;
        }
        super.invalidate();
    }

    /**
     * @return 获取圆角大小
     */
    public float getRoundBorderRadius() {
        return mRoundBorderRadius;
    }

    /**
     * @param mRoundBorderRadius 设置圆角
     */
    public void setRoundBorderRadius(int mRoundBorderRadius) {
        if (this.mRoundBorderRadius != mRoundBorderRadius) {
            this.mRoundBorderRadius = Resources.getSystem().getDisplayMetrics().density *
                    mRoundBorderRadius;
            invalidate();
        }
    }

    /**
     * @return 获取类型
     */
    public int getType() {
        return this.mType;
    }

    /**
     * @param mType 设置类型
     */
    public void setType(int mType) {
        if (this.mType != mType) {
            this.mType = mType;
            invalidate();
        }
    }
}